//音频
$(".btn-frequency")
                .parent("li:first")
                .click(
                                function() {
                                        currentClickTextId = -1;// 标记当前选中的不是textarea;
                                        currentClickImageId = -1;
                                        currentClickVideoId = -1;
                                        
                                        // 循环ID
                                        radioId++;
                                        currentClickRadioId = radioId;
                                        var radiostr = "<div id='radio-"
                                                        + radioId
                                                        + "' style='position: absolute;left:"+pptLeft+"px;top:"+pptTop+"px;'>"
                                                        + "<div id='radio-title-"
                                                        + radioId
                                                        + "' class='radio-tit'><span><img src='../images/ppt/icon01.png' class='v-middle'/>&nbsp;<strong>点击这里拖动</strong>&nbsp;</span>"
                                                        +"<img src='../images/ppt/error_tips.png' class='image-delete v-middle' onclick='divDelete(this);'/></div>"
                                                        + "<img id='radio-img-"+radioId+"' src='../images/ppt/4.png' width='31px' height='31px' />"
                                                        + "</div>";
                                        $(".c-content").append(radiostr);

                                        $(".radio-layout").fadeIn("slow");

                                        radioListener(radioId);

                                        updateRadioLayout(radioId);
                                        
                                        //更新
                                        ppt.reSave();
                                        
                                        $("#uploadRadio").val("");
                                });

/**
 * 音频监听事件
 */
function radioListener(radioId) {
        // 音频和配置数据都赋予拖拽功能
        $("#radio-" + radioId).draggable({
                //containment: ".c-content"
        });
        
        $(".radio-layout").draggable();
        
        //标题
        $("#radio-title-"+radioId).click().mousedown(function(){
                isMouseDown=true;
                currentClickRadioId = radioId;
                currentClickTextId = -1;
                currentClickImageId = -1;
                currentClickVideoId = -1;
                
                $("#radio-"+radioId).fadeTo(10,0.5);
                $("#radio-"+radioId).css("z-index", "" + (++zindex));
                updateRadioLayout(radioId);
        });
        
        $("#radio-title-"+radioId).click().mouseup(function(){
                isMouseDown=false;
                $("#radio-"+radioId).fadeTo(10,1);
                updateRadioLayout(radioId);
        });
        
        
        $("#radio-title-"+radioId).mousemove(function(){
                if(isMouseDown){
                        updateRadioLayout(radioId);
                }
        });
        
        $("#radio-title-"+radioId).click().mouseout(function(){
        	$("#radio-"+radioId).fadeTo(10,1);
        });
        
        
        $("#radio-img-"+radioId).mousemove(function(){
                if(isMouseDown){
                        updateRadioLayout(radioId);
                }
        });
        
        $("#radio-img-"+radioId).mousedown(function(){
                isMouseDown=true;
                updateRadioLayout(radioId);
        });
        
        $("#radio-img-"+radioId).mouseup(function(){
                isMouseDown=false;
                updateRadioLayout(radioId);
        });
}

/**
 * 音频上传后确认更改
 */
$("#radio-upload-submit").click(
                function() {
                        if (currentClickRadioId == -1) {
                                alert("请选 中一个音频框体");
                                return;
                        }
                        var w = "200", h = "100", autostart, loop;

                        try {
                                w = parseInt($("#radio-width").val());
                                h = parseInt($("#radio-height").val());
                        } catch (e) {
                                alert("请输入数字");
                                $("#radio-width").val("100");
                                $("#radio-height").val("100");
                        }

                        if ($("#radio-aotustart").attr("checked") == "checked") {
                                autostart = true;
                        } else {
                                autostart = false;
                        }

                        if ($("#radio-loop").attr("checked") == "checked") {
                                loop = true;
                        } else {
                                loop = false;
                        }

                        var src = $("#radioUploadSrc").val();

                        if (src != "") {
                                var html = "<embed src='" + src + "' id='radio-content-"
                                                + currentClickRadioId
                                                + "' type='video/x-ms-asf-plugin' width='" + w
                                                + "' height='" + h + "' autostart='" + autostart
                                                + "' loop='" + loop + "' />";
                                //删除音频图标
                                $("#radio-"+currentClickRadioId+">img").remove();
                                $("#radio-"+currentClickRadioId+">embed").remove();
                                $("#radio-" + currentClickRadioId).append(html);
                        } else {
                                alert("请输入有效音频链接或者上传音频文件");
                        }
                });

/**
 * 更新属性框信息
 * 
 * @param radioId
 */
function updateRadioLayout(radioId) {

        var newpos = adjustPosition(".c-content", "#radio-" + radioId, "#radio-content-" + radioId);
        
        var x = (isNaN(newpos.left) ? newpos.pleft : newpos.left);
        var y = (isNaN(newpos.top) ? newpos.ptop : newpos.top);;
        
        $("#radio-x-coordinate").val(parseInt(x - newpos.pleft));
        $("#radio-y-coordinate").val(parseInt(y - newpos.ptop));

        // 更改位置 x y
        $("#radio-" + radioId).css("left", x);
        $("#radio-" + radioId).css("top", y);
        
        //自动播放
        var check = $("#radio-content-" + radioId).attr("autostart");
        if(check=="true"){
                $("#radio-aotustart").attr("checked",true);
        }
        else{
                $("#radio-aotustart").attr("checked",false);
        }
        
        //循环
        var check = $("#radio-content-" + radioId).attr("loop");
        if(check=="loop"){
                $("#radio-loop").attr("checked",true);
        }
        else{
                $("#radio-loop").attr("checked",false);
        }
        
        var src = $("#radio-content-"+radioId).attr("src");
        $("#radioUploadSrc").val(src);

}

/**
 * 上传时验证
 * 
 * @returns {Boolean}
 */
function checkRadio() {
        var radiofile = $("#uploadRadio").val();

        if (radiofile == undefined || radiofile == "" || radiofile == null) {
                alert("请选择要上传的音频：waf、mp3、wma、wav、acc");
                return false;
        }

        var index = radiofile.lastIndexOf("\\");
        var name = radiofile.substr(index + 1, radiofile.length);
        index = name.lastIndexOf(".");
        var suffix = name.substr(index + 1, name.length).toLowerCase();

        if (suffix == "waf" || suffix == "mp3" || suffix == "wma"
                        || suffix == "wav" || suffix == "acc") {
                return true;
        }

        alert("只能上传waf、mp3、wma、wav、acc格式的音频");
        return false;
}

/**
 * 点击自动播放
*/

$("#radio-aotustart").click(function(){
        var check = $(this).attr("checked");
        
        if(check=="checked" || check==true){
                $("#radio-content-"+currentClickRadioId).attr("autostart",true);
        }
        else{
                $("#radio-content-"+currentClickRadioId).attr("autostart",false);
        }
});

//循环
$("#radio-loop").click(function(){
        var check = $(this).attr("checked");
        
        if(check=="checked" || check==true){
                $("#radio-content-"+currentClickRadioId).attr("loop",true);
        }
        else{
                $("#radio-content-"+currentClickRadioId).attr("loop",false);
        }
});



/**
 * 上下居中
 */
$("#radio-up-down-center").click(function(){
    if(currentClickRadioId==-1){
        alert("请选 中一个音频框体");
        return;
    }
	
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#radio-" + currentClickRadioId, "#radio-content-" + currentClickRadioId);
    var h = newpos.h;
    
    //居中时左上角坐标，只要考虑top
    var newtop = parseInt((newpos.ptop+newpos.pendtop)/2-h/2)-9;
    $("#radio-" + currentClickRadioId).css("top", newtop);
    $("#radio-y-coordinate").val(parseInt(newtop - newpos.ptop));
    
});


/**
 * 左右居中
 */
$("#radio-left-right-center").click(function(){
    if(currentClickRadioId==-1){
        alert("请选 中一个音频框体");
        return;
    }
	//得到图片的左上角坐标
    var newpos = adjustPosition(".c-content", "#radio-" + currentClickRadioId, "#radio-content-" + currentClickRadioId);
    var w = newpos.w;
    
    //居中时左上角坐标，只要考虑left
    var newleft = parseInt((newpos.pleft+newpos.pendleft)/2-w/2);
    $("#radio-" + currentClickRadioId).css("left", newleft);
    $("#radio-x-coordinate").val(parseInt(newleft - newpos.pleft));
});